local sifeng = fk.CreateSkill {
  name = "xiaobai__sifeng",
  dynamic_desc = function(self, player)
        if player.dead then
          return self.name.."_inner"
        else
          return self.name
        end
      end,
}
local U = require "packages.utility.utility"
Fk:loadTranslationTable{
  ["xiaobai__sifeng"]="司酆",
  [":xiaobai__sifeng"]="你曾登场过的额定回合开始前，你可以令当前回合角色选择一项：1.交给「你」一张牌；2.「你」对其发动对应的〖显灾〗效果。若其已死亡，你令一名角色执行此额定回合；若你已死亡，你依然可以发动此技能，但选项中的「你」改为「一号位」。",
  [":xiaobai__sifeng_inner"]="你曾登场过的额定回合开始前，你可以令当前回合角色选择一项：1.交给「一号位」一张牌；2.「一号位」对其发动对应的〖显灾〗效果。若其已死亡，你令一名角色执行此额定回合；若你已死亡，你依然可以发动此技能。",
  ["#xiaobai__sifeng_trigger-invoke"]="是否发动 司酆 令 %dest 选择交给「你」牌或「你」对其发动对应〖显灾〗效果（%arg）",
  ["#xiaobai__sifeng_trigger-dead-invoke"]="是否发动 司酆 令 %dest 选择交给「一号位」牌或「一号位」对其发动对应〖显灾〗效果",
  ["#xiaobai__sifeng_giveturn-choose"]="司酆：你须令一名角色执行属于 %dest 的额定回合",
  ["#xiaobai__sifeng_givecard-choose"]="司酆：交给 %dest 一张牌，或者不交（取消）然后其对你发动对应你的〖显灾〗效果",

  ["$xiaobai__sifeng1"]="予，掌六天宫之印绶，总十阴司之纪纲。",
  ["$xiaobai__sifeng2"]="前尘俱渺，业果自分，实乃天道示森然。",
  ["$xiaobai__sifeng3"]="（死亡时）予，掌六天宫之印绶，总十阴司之纪纲。",
  ["$xiaobai__sifeng4"]="（死亡时）前尘俱渺，业果自分，实乃天道示森然。",
  
}
sifeng:addEffect(fk.PreTurnStart,{
  anim_type = "control",
  mute = true,
  late_refresh = true,
  can_trigger= function(self, event, target, player, data)
    if player:hasSkill(sifeng.name,false,true) then
      local trueseat = target.tag["xiaobai__sifeng_originSeat"] or 0
      --print("当前座次为"..trueseat)
      local seatnumber = target.seat
      if trueseat > 0  then
        seatnumber = trueseat
      end
      local mark =  player.tag["xiaobai__xianzai-seatsandevent"] or {}
      if #mark>0 then
        for _, tos in ipairs(mark) do
          if tos[1] == seatnumber then
            return player.room.current and player.room.current:isAlive()
          end
        end  
      end
  end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player.dead then
      return room:askToSkillInvoke(player, {
        skill_name = sifeng.name,
        prompt = "#xiaobai__sifeng_trigger-dead-invoke::"..room.current.id,
      })
    else
    return room:askToSkillInvoke(player, {
        skill_name = sifeng.name,
        prompt = "#xiaobai__sifeng_trigger-invoke::"..room.current.id,
      })
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player.dead then
      player:broadcastSkillInvoke(sifeng.name, math.random(3,4))
    else
      player:broadcastSkillInvoke(sifeng.name, math.random(1,2))
    end
    room:notifySkillInvoked(player, sifeng.name)
    room:doIndicate(player,{room.current})
    local fromwho = player
    if player.dead then fromwho = table.filter(room.alive_players,function(p) return p.seat == 1 end)[1] end
    local id = {}
    if room.current ~= fromwho then
     id = room:askToCards(room.current, {
      skill_name = sifeng.name,
      include_equip = true,
      min_num = 1,
      max_num = 1,
      cancelable = true,
      prompt = "#xiaobai__sifeng_givecard-choose::"..fromwho.id,
    })
  end
    if #id>0 then
      room:moveCardTo(id, Player.Hand, fromwho, fk.ReasonPrey, sifeng.name, nil, true, room.current)
    else
      local trueseat = target.tag["xiaobai__sifeng_originSeat"] or 0
      local seatnumber = room.current.seat
      if trueseat > 0  then
        seatnumber = trueseat
      end
      local mark =  player.tag["xiaobai__xianzai-seatsandevent"] or {}
      if #mark>0 then
        for _, tos in ipairs(mark) do
          if tos[1] == seatnumber then
            if tos[2] == 1 then
              room.current:drawCards(2,"xiaobai__xianzai")
              if room.current:isAlive() and #room.current:getCardIds('h')>0 then
        local id = room:askToCards(room.current, {
      skill_name = "xiaobai__xianzai",
      include_equip = false,
      min_num = 1,
      max_num = 1,
      prompt = "显灾：你须明置一张手牌，视为【毒】",
      cancelable = false,
    })[1]
    if id then
      local card = Fk:getCardById(id)
      room:setCardMark(card, "@@xiaobai__xianzai-poison-inarea", 1)
    end
  end
            elseif tos[2] == 2 then
              for _ = 1, 999 do
                if room.current:isAlive() and not room.current.chained then
        room.current:setChainState(true)
                end
        if room.current:isAlive() and fromwho:isAlive() then
        local use = room:useVirtualCard("fire_attack", nil, fromwho,{room.current}, "xiaobai__xianzai", true)
        if not use or not use.damageDealt then break end
        else break end
    end
            elseif tos[2] == 3 then
              local num = 4 - fromwho:getHandcardNum()
    if num > 0 then
      fromwho:drawCards(num, "xiaobai__xianzai")
    end
    if fromwho:isAlive() and room.current and room.current:isAlive() then
      room:swapAllCards(fromwho,{fromwho,room.current}, "xiaobai__xianzai")
    end
            end
          end
        end  
      end
    end
  end,
})
sifeng:addEffect(fk.PreTurnStart,{
  anim_type = "control",
  late_refresh = true,
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    target.tag["xiaobai__sifeng_originSeat"] = 0
  end,
})
sifeng:addEffect(fk.EventTurnChanging, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(sifeng.name,false,true) and data.to and data.to.dead then
      local seatnumber = data.to.seat
      local mark =  player.tag["xiaobai__xianzai-seatsandevent"] or {}
      if #mark>0 then
        for _, tos in ipairs(mark) do
          if tos[1] == seatnumber then
            return true
          end
        end  
      end
  end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player.dead then
      player:broadcastSkillInvoke(sifeng.name, math.random(3,4))
    else
      player:broadcastSkillInvoke(sifeng.name, math.random(1,2))
    end
    room:notifySkillInvoked(player,sifeng.name,"special")
    local to = room:askToChoosePlayers(player, {
        targets = room.alive_players,
        min_num = 1,
        max_num = 1,
        prompt = "#xiaobai__sifeng_giveturn-choose::"..data.to.id,
        skill_name = sifeng.name,
        cancelable = false,
      })
      if #to >0 then
        to[1].tag["xiaobai__sifeng_originSeat"] = data.to.seat
    data.to = to[1]
      end
  end,
})
sifeng:addEffect(fk.Deathed, {
  can_refresh = function (self, event, target, player, data)
    if target == player then
    return (player.general == "xiaobai__jiangziwen") or (player.deputyGeneral == "xiaobai__jiangziwen")
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player.general == "xiaobai__jiangziwen" then
    player.general = "xiaobai__jiangziwen_dead"
    room:broadcastProperty(player, "general")
    else
    player.deputyGeneral = "xiaobai__jiangziwen_dead"
    room:broadcastProperty(player, "deputyGeneral")
    end
    room:changeKingdom(player, "god", false)
  end,
})
sifeng:addEffect(fk.AfterPlayerRevived, {
can_refresh = function (self, event, target, player, data)
    if target == player then
    return (player.general == "xiaobai__jiangziwen") or (player.deputyGeneral == "xiaobai__jiangziwen")
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player.general == "xiaobai__jiangziwen_dead" then
    player.general = "xiaobai__jiangziwen"
    room:broadcastProperty(player, "general")
    else
    player.deputyGeneral = "xiaobai__jiangziwen"
    room:broadcastProperty(player, "deputyGeneral")
    end
    if player.kingdom == "god" then
      room:changeKingdom(player, "wu", false)
    end
  end,
})
return sifeng